from django.shortcuts import render_to_response
from django.http import HttpResponse
from computermanage.models import *
import datetime
from datetime import *
from time import gmtime, strftime
from django.http import HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt


def main(request):
    return render_to_response('mainuser.html')


def inform(request):
    return render_to_response('inform_request.html')
def r_inform(request):
    try:      
        upst = Computer.objects.get(comp_ID='%s'%request.GET['comid'])
        message = 'You inform for : %s <br><br><a href=http://127.0.0.1:8080>Back to main page</a>' % request.GET['comid']
        com =  Request.objects.create(
            request_Date = '%s' % date.today(),
            request_Time = '%s' % strftime("%H:%M:%S"),
            request_Com_ID = '%s' % request.GET['comid'],
            requset_ComProb = '%s' % request.GET['problem'],
            request_Status = 'new',)
        com.save()
        upst.comp_Status = 'offline'
        upst.save()
    except Computer.DoesNotExist:
        message = 'No your computer in database'        
    return HttpResponse(message)




def comreport(request): 
    return render_to_response('complete-report.html')
def complete(request):
    completeid = request.GET['completeid']
    rep =  Computer.objects.get(comp_ID=completeid)
    rep.comp_Status = 'online'
    count = int(rep.comp_Fixcount)
    count+=1
    rep.comp_Fixcount = '%s'%count
    rep.save()
    his = History.objects.create(
            his_Date = '%s' % date.today(),
            his_Time = '%s' % strftime("%H:%M:%S"),
            his_ComID = completeid,
            his_Fixer = 'fixername')
    his.save()
    Request.objects.filter(request_Com_ID=completeid).update(request_Status = 'complete')
    return HttpResponse('Report has been send<br><br><a href=http://127.0.0.1:8080/fixer/>Back to main page</a>')

    
def fixer(request): 
    return render_to_response('fixerpage.html')
def ch_inform(request):
    show = Request.objects.all()
    return render_to_response('view-inform.html',{'show':show})



def login(request):
    return render_to_response('login.html')
@csrf_exempt
def r_login(request):
    if request.POST['logintype']=='Fixer':
        try:      
            auth = Fixer.objects.get(fixer_ID='%s'%request.POST['MM_user'],
                                     fixer_Password='%s'%request.POST['MM_pass'],
                                     )
            return HttpResponseRedirect('/fixer/')
        except Fixer.DoesNotExist:
            message = 'No this ID and Password in database'
            return HttpResponse(message)
    else:
        return HttpResponse('underconstruction')      









